package com.ruoyi.onway.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.onway.mapper.ReportOnwayMapper;
import com.ruoyi.onway.domain.ReportOnway;
import com.ruoyi.onway.service.IReportOnwayService;

/**
 * 在途油信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class ReportOnwayServiceImpl implements IReportOnwayService 
{
    @Autowired
    private ReportOnwayMapper reportOnwayMapper;

    /**
     * 查询在途油信息
     * 
     * @param id 在途油信息ID
     * @return 在途油信息
     */
    @Override
    public ReportOnway selectReportOnwayById(Long id)
    {
        return reportOnwayMapper.selectReportOnwayById(id);
    }

    @Override
    public void checkOnway(ReportOnway reportOnway) {
        Long planId = reportOnway.getPlanId();
        //查询采购计划关联的 该炼厂 分配量
        Long refineryId = reportOnway.getRefineryId();
        if (ObjectUtil.isNull(planId)){
            throw new RuntimeException("在途油信息必须关联采购计划");
        }
        if (ObjectUtil.isNull(refineryId)){
            throw new RuntimeException("在途油信息必须选择炼厂信息");
        }
      BigDecimal total= reportOnwayMapper.selectPlanRefinerySumQuantity(planId,refineryId);
       total=ObjectUtil.isNotNull(total)?total:new BigDecimal(0);

        BigDecimal sendWeight = ObjectUtil.isNotNull(reportOnway.getSendWeight())?reportOnway.getSendWeight():BigDecimal.ZERO;

       BigDecimal other= reportOnwayMapper.selectSumQuantity(reportOnway);
        other=ObjectUtil.isNotNull(other)?other:new BigDecimal(0);
       if (other.add(sendWeight).compareTo(total)>0){
           throw new RuntimeException("该炼厂分配的在途油已超过采购计划给该炼厂分配总量");
       }
    }

    /**
     * 查询在途油信息列表
     * 
     * @param reportOnway 在途油信息
     * @return 在途油信息
     */
    @Override
    public List<ReportOnway> selectReportOnwayList(ReportOnway reportOnway)
    {
        return reportOnwayMapper.selectReportOnwayList(reportOnway);
    }

    /**
     * 新增在途油信息
     * 
     * @param reportOnway 在途油信息
     * @return 结果
     */
    @Override
    public int insertReportOnway(ReportOnway reportOnway)
    {
        this.checkOnway(reportOnway);
        reportOnway.setCreateTime(DateUtils.getNowDate());
        return reportOnwayMapper.insertReportOnway(reportOnway);
    }

    /**
     * 修改在途油信息
     * 
     * @param reportOnway 在途油信息
     * @return 结果
     */
    @Override
    public int updateReportOnway(ReportOnway reportOnway)
    {
        this.checkOnway(reportOnway);
        reportOnway.setUpdateTime(DateUtils.getNowDate());
        return reportOnwayMapper.updateReportOnway(reportOnway);
    }

    /**
     * 批量删除在途油信息
     * 
     * @param ids 需要删除的在途油信息ID
     * @return 结果
     */
    @Override
    public int deleteReportOnwayByIds(Long[] ids)
    {
        return reportOnwayMapper.deleteReportOnwayByIds(ids);
    }

    /**
     * 删除在途油信息信息
     * 
     * @param id 在途油信息ID
     * @return 结果
     */
    @Override
    public int deleteReportOnwayById(Long id)
    {
        return reportOnwayMapper.deleteReportOnwayById(id);
    }
}
